package kr.or.kead.ui.insert_update;

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import kr.or.kead.domain.Depart;
import kr.or.kead.module.TelCheck;
import kr.or.kead.service.DaoProfessor;
import kr.or.kead.service.Daodepart;

public class DepartInsrtUpdate extends AbsInsertUpdate {
	

	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JTextField txtName;
	private JLabel lblNewLabel_2;
	private JComboBox<Object> comboProf;
	private JLabel lblNewLabel_3;
	private TelCheck txtTel;
	private JLabel labelDepartCode;
	private Daodepart daoTable;
	private DaoProfessor daoProfessor;
	public DepartInsrtUpdate(Object obj) {
		super(obj, "학과관리");
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void initDao() {
		daoTable = new Daodepart();  
		daoProfessor = new DaoProfessor();
	}

	@Override
	protected boolean isValidCheck() {
		if(txtName.getText().equals("")||txtTel.getphone().equals("")){
			JOptionPane.showMessageDialog(null, "비어있는 란이 있습니다!");
			txtName.requestFocus();
			return false;
		}
		return true;
	}

	@Override
	protected JPanel getMainPanel() {
		JPanel cp = new JPanel();
		cp.setLayout(new GridLayout(4, 2, 0, 0));
		
		lblNewLabel = new JLabel("학과코드 : ");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		cp.add(lblNewLabel);
		
		labelDepartCode = new JLabel(String.valueOf(((Daodepart) daoTable).selectMaxCode()+100));
		
		cp.add(labelDepartCode);
		
		lblNewLabel_1 = new JLabel("학과명 : ");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		cp.add(lblNewLabel_1);
		
		txtName = new JTextField();
		cp.add(txtName);
		txtName.setColumns(10);
		
		lblNewLabel_2 = new JLabel("교수명 :");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		cp.add(lblNewLabel_2);
		ArrayList<String> arList = daoProfessor.selectTableAllList();
		Object[] objList = arList.toArray();
		comboProf = new JComboBox<>(objList);
		cp.add(comboProf);
		
		lblNewLabel_3 = new JLabel("연락처 : ");
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		cp.add(lblNewLabel_3);
		
		txtTel = new TelCheck(false);
		cp.add(txtTel);
		return cp;
	}

	@Override
	protected Object getObject() {
		String prof = (String) comboProf.getSelectedItem();
		StringTokenizer st = new StringTokenizer(prof, ":");
		if (obj==null){// 추가
			Depart depart = new Depart(
					Integer.parseInt(labelDepartCode.getText()),
					txtName.getText(), 
					Integer.parseInt(st.nextToken().trim()), 
					txtTel.getphone());
			return depart;
		}else{//수정
			Depart depart = (Depart) obj;
			depart.setCode(Integer.parseInt(labelDepartCode.getText()));
			depart.setName(txtName.getText());
			depart.setProt(Integer.parseInt(st.nextToken().trim()));
			depart.setTel(txtTel.getphone());
			return depart;
		}
	}

	@Override
	protected void updateInit() {
		Depart depart = (Depart) obj;
		labelDepartCode.setText(depart.getCode()+"");
		txtName.setText(depart.getName());
		//차후 수정
		comboProf.setSelectedItem(daoProfessor.selectCodeNameByid(depart.getProt()));;
		txtTel.setphone(depart.getTel());
	}

}
